﻿package com.dg.gaming.client {
    import com.dg.gaming.api.common.AbstractConnectionThread;
    import com.dg.gaming.api.common.Game;
    import com.dg.gaming.api.common.MessageEvent;
    import com.dg.gaming.api.common.Player;
    import com.dg.gaming.api.common.Table;
    import com.dg.gaming.api.common.msg.ITransmittable;
    import com.dg.gaming.api.common.msg.MsgGameMessage;
    import com.dg.gaming.api.common.msg.MsgTableMessage;
    import com.dg.gaming.api.common.util.DGUtils;

    import flash.events.ProgressEvent;
    import flash.net.Socket;

    import nanoxml.XMLElement;

    public class TableConnection extends AbstractConnectionThread {

        private static var instance:TableConnection;

        /** Interface betweem this thread and the Frame */
        protected var clientInterface:IClient;

        /** Current game ID. */
        protected var gameID:String = null;

        /** Socket between the server and the user. */
        protected var socket:Socket;


        private var table:Table;

        private var tableConn:TableConnectionThread;

        private var userName:String;

        private var player:Player;

        public static function getInstance():TableConnection {
            return instance;
        }

        /**
         * Default constructor which takes a Socket connection to the server, a
         * username and an IClient which sits between this class and the GUI.
         *
         * @param connection       Socket connection to server.
         * @param username         Username
         * @param clientInterface  Interface between this class and GUI.
         */
        public function TableConnection(connection:Socket, userid:Number, clientInterface:IClient) {
            super();
            instance = this;
            this.clientInterface = clientInterface;

            this.userid = userid;
            try {
                setSocket(connection);
            } catch (ex:Exception) {
            }

        }

        public function inputDataHandler(event:ProgressEvent):void {

            var bytesLoaded:int = event.bytesLoaded;
            var str:String = socket.readUTFBytes(bytesLoaded);
            trace(str);
        }

        protected function setSocket(socket:Socket):void {
            this.socket = socket;

//			if (socket != null)
//			{
//				socket.addEventListener(ProgressEvent.SOCKET_DATA, inputDataHandler);
//			}
        }


        /**
         * Set the client interface.
         *
         * @param clientInterface  Client interface
         */
        public function setClientInterface(clientInterface:IClient):void {
            this.clientInterface = clientInterface;
        }

        public function getPlayer():Player {
            return player;
        }

        public function setTable(table:Table):void {
            this.table = table;
            this.tableConn = new TableConnectionThread(this, table.getTableNum());
            var game:Game = table.getGame();
            game.getTableList().addTableObj(table);
            player = table.getPlayerList().getPlayer(userid);

        }

        public function setUserName(userName:String):void {
            this.userName = userName;
        }

        /**
         * Parse method which reads the first token of the message and delegate
         * to the implementating client.
         *
         * @see com.dg.gaming.api.common.AbstractConnectionThread#parse(nanoxml.XMLElement)
         */
        public override function parse(message:XMLElement):void {
            dispatchEvent(new MessageEvent(message));
        }

        /**
         * Send a ITransmittable object from a client to the server.
         *
         * @param message
         */
        public function sendGameMessage(message:MsgGameMessage):void {
            this.send(ITransmittable(message));
        }



        public override function send(transObject:ITransmittable):void {
            if (transObject is MsgTableMessage) {
                MsgTableMessage(transObject).setTableNum(table.getTableNum());
            }
            var message:String = transObject.flatten().toString();
            socket.writeBytes(DGUtils.enscriptBytes(message));
            socket.flush();
        }

        public override function cleanup():void {
            // Clean up
        }

        public function getTable():Table {
            return table;
        }

        public function getTableConn():TableConnectionThread {
            return tableConn;
        }

        public function getUserName():String {
            return userName;
        }

        /**
         * getTableNum
         *
         * @return String
         */
        public function getTableNum():String {
            return table.getTableNum();
        }

        /**
         * getGame
         *
         * @return Game
         */
        public function getGame():Game {
            return table.getGame();
        }

    }
}